<template>
  <div class="app-container">
    <div class="filter-container clearfix">
      <el-select v-model="listQuery.sid" class="filter-item" style="width: 110px;" clearable filterable :loading="loading" placeholder="选择区服">
        <el-option v-for="item in serverList" :key="item.sid" :label="item.name+'('+item.sid+')'" :value="item.sid"></el-option>
      </el-select>
      <el-input v-model="listQuery.actor_id" class="filter-item" style="width: 160px;" placeholder="角色ID" />
      <el-date-picker v-model="listQuery.start_date" class="filter-item" type="date" style="width: 140px;" placeholder="开始日期"></el-date-picker>-
      <el-date-picker v-model="listQuery.end_date" class="filter-item" type="date" style="width: 140px;" placeholder="结束日期"></el-date-picker>
      <el-button class="filter-item" type="primary" plain icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" border stripe style="width: 100%;" height="740">
      <el-table-column label="服务器" prop="sname" width="100px" align="center"></el-table-column>
      <el-table-column label="账号" prop="account" width="150px" align="center"></el-table-column>
      <el-table-column label="角色ID" prop="actor_id" width="110px" align="center"></el-table-column>
      <el-table-column label="角色名" prop="actor_name" align="center"></el-table-column>
      <el-table-column label="等级" prop="level" width="60px" align="center"></el-table-column>
      <el-table-column label="VIP" prop="vip" width="60px" align="center"></el-table-column>
      <el-table-column label="总充值金额" prop="amount" width="100px" align="center"></el-table-column>
      <el-table-column label="真实充值" prop="gm_amount" align="center"></el-table-column>
      <el-table-column label="英雄ID" prop="hero_id" width="90px" align="center"></el-table-column>
      <el-table-column label="监控值" prop="num" width="110px" align="center"></el-table-column>
      <el-table-column label="日期" prop="create_dt" width="140px" align="center"></el-table-column>
      <el-table-column label="操作" width="100px" align="center">
        <template slot-scope="{row}">
          <router-link :to="'/gm/actor/list/' + row.sid + '/' + row.actor_id" class="filter-item">
            <el-button type="text">查看</el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialogVisible" title="监控配置" :close-on-click-modal="false">
      <el-form ref="dataForm" :model="dataForm" label-width="100px" label-position="left">
        <el-form-item label="监控开关">
          <el-switch v-model="dataForm.status" active-text="开" inactive-text="关" active-value="1" inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="消息通知开关">
          <el-switch v-model="dataForm.notify" active-text="开" inactive-text="关" active-value="1" inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="服务器范围">
          <el-radio v-model="dataForm.scope" label="1">全部服</el-radio>
          <el-radio v-model="dataForm.scope" label="0">正式服</el-radio>
        </el-form-item>
        <el-form-item label="配置">
          <el-row>
            <el-col :md="3"><el-button type="primary" class="add" icon="el-icon-plus" @click="productAdd">新增</el-button></el-col>
          </el-row>
          <el-row v-for="(item, index) in dataForm.items" :key="index" class="mt-5" :gutter="12">
            <el-col :md="6">
              <el-select v-model="item.id" clearable filterable placeholder="请选择物品">
                <el-option v-for="p in products" :key="p.id" :label="p.desc" :value="p.id" />
              </el-select>
            </el-col>
            <el-col :md="8"><el-input v-model="item.num" placeholder="监控值" /></el-col>
            <el-col :md="4"><el-button type="danger" icon="el-icon-delete" @click.prevent="remove(item)" /></el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <div class="tr">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSubmit()">确定</el-button>
      </div>
    </el-dialog>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import checkPermission from '@/utils/permission' // 权限判断函数
import waves from '@/directive/waves' // 实现点击波浪纹
import { deepClone } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { handleMonitorConfig, getMonitorPlayerHeroList, getMonitorConfig } from '@/api/monitor'
import { getItemConfig } from '@/api/config'
import { getServerList } from '@/api/ops'

const defaultItem = {
  status: '0',
  notify: '0',
  scope: '0',
  items: [{
    id: '',
    num: ''
  }]
}
export default {
  name: 'PlayerHero',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      permissionRoles: this.$store.state.user.permissionRoles,
      list: [],
      serverList: [],
      products: [],
      items: [],
      total: 0,
      loading: false,
      listLoading: false,
      dataForm: Object.assign({}, defaultItem),
      listQuery: {
        sid: '',
        actor_id: '',
        start_date: new Date(Date.now() - 86400000 * 7),
        end_date: new Date(),
        page: 1,
        limit: 200
      },
      dialogVisible: false
    }
  },
  created() {
    this.getList()
    this.getServerList()
  },
  methods: {
    checkPermission,
    async getList() {
      this.listLoading = true
      const res = await getMonitorPlayerHeroList(this.listQuery)
      this.listLoading = false
      this.list = res.data.items
      this.total = res.data.total
      this.items = res.data.item
    },
    handleFilter() {
      this.getList()
    },
    handleAdd() {
      this.getItemConfig()
      this.getMonitorConfig()
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    confirmSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          handleMonitorConfig(this.dataForm).then(response => {
            if (response.status) {
              this.dialogVisible = false
              this.getList()
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
            } else {
              this.message.error(response.message)
            }
          })
        }
      })
    },
    async getMonitorConfig() {
      const res = await getMonitorConfig()
      const row = {}
      const items = res.data
      const a = []
      for (let i = 0; i < items.length; i++) {
        const o = { id: '', num: '' }
        o.id = items[i].item_id
        o.num = items[i].num
        a[i] = o
        row.status = items[i].status + ''
        row.notify = items[i].notify
        row.scope = items[i].scope
      }
      row.items = a
      this.dataForm = deepClone(row)
    },
    async getItemConfig() {
      const res = await getItemConfig()
      this.products = res.data.items
    },
    productAdd() {
      this.dataForm.items.push({
        id: '',
        num: ''
      })
    },
    remove(row) {
      var index = this.dataForm.items.indexOf(row)
      if (index !== -1) {
        this.dataForm.items.splice(index, 1)
      }
    },
    async getServerList(params) {
      const query = params
      const res = await getServerList(query)
      this.serverList = res.data.items
      this.loading = false
    }
  }
}
</script>
